<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="配置文件">
      <el-upload
        class="upload-demo"
        :auto-upload="false"
        action="#"
        :limit="1"
        :on-change="handleChange"
        list-type="text"
        :before-upload='handleBeforeUpload'
        :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">文件大小不超过1M</div>
      </el-upload>
    </el-form-item>
    <el-form-item size="large">
      <el-button type="primary" @click="onSubmit(form)">立即重置</el-button>
      <el-button @click="handleCancelResetPassWord">返回</el-button>
    </el-form-item>
  </el-form>
</template>


<script>
import {uploadResetPassWordFile} from "@/api/login";

export default {
  data() {
    return {
      fileList: [],
      form: {
        file: '',
      }
    };
  },
  methods: {
    onSubmit() {
      // console.log('this.refs',this.$refs)
      this.$refs.form.validate((valid) => {
        if (valid) {
          // alert('submit!');
          const params = new FormData();
          // params.append("personId", this.personId);
          params.append("fileData", this.fileList[0].raw);
          uploadResetPassWordFile(params).then(res => {
            this.$message.success("已经重置为出厂密码");
            console.log(res);
          }).catch(err => {
            console.log(err);
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleChange(file, fileList) {
      console.log('ffff', file)
      const isLt1M = file.size / 1024 / 1024 < 1
      if(!isLt1M){
        this.$message.error(`不能超过1M`);
        // this.fileList.splice(0,1)
        this.fileList = []
        console.log(this.fileList)
        return false
      }
      this.fileList = []
      this.fileList = fileList
      // console.log('this.fileList',this.fileList)

    },
    handleBeforeUpload(files, fileList) {
      console.log('files', files)
      // this.$message.error(`只允许上传单个文件`);

      // console.log(file)
      // this.fileList = []
      // this.fileList = fileList
      // console.log('this.fileList',this.fileList)

    },
    handleCancelResetPassWord() {
      console.log('cancel!');
      this.$emit("handleCancelResetPassWord")
    }
  }
};
</script>
